<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <style>
        .center{
            width: 1200px;
            margin: 0 auto;
        }
        #footer_center p{
            margin: 0;
        }
        #footer_center h3{
            color: white;
        }
        body{
            background-color: rgb(241,242,243);
        }
        .el-header{
            background-color: white;
        }
    </style>
</head>
<body>
<div id="app">
    <el-container>
        <my-header></my-header>
        <el-main class="center" >
            <el-row gutter="20">
                <el-col span="4">
                    <el-card style="height: 500px">
                        <p style="color: orange">个人信息</p>
                        <p @click="location.href='articleManagement.html'">稿件管理</p>
                    </el-card>
                </el-col>
                <el-col span="20">
                    <el-card style="height: 500px">
                        <el-form label-width="100px">
                            <el-form-item label="头像">
                                <img :src="user.imgUrl"
                                     style="width: 145px;height: 145px;
                            border-radius: 10px;float: left">
                                <!--**********上传组件开始***********-->
                                <!--action:设置请求地址-->
                                <el-upload style="float: left"
                                        action="/v1/upload"
                                           name="file"
                                           limit="1"
                                        list-type="picture-card"
                                           :on-success="handleSuccess"
                                        :on-preview="handlePictureCardPreview"
                                        :on-remove="handleRemove">
                                    <i class="el-icon-plus"></i>
                                </el-upload>
                                <el-dialog :visible.sync="dialogVisible">
                                    <img width="100%" :src="dialogImageUrl" alt="">
                                </el-dialog>
                                <!--**********上传组件结束***********-->
                            </el-form-item>
                            <el-form-item label="昵称">
                                <el-input placeholder="请输入昵称" v-model="user.nickName"
                                          style="width: 200px"></el-input>
                            </el-form-item>
                            <el-form-item label="用户名">
                                <el-input disabled :value="user.username"
                                          style="width: 200px"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" @click="save()">保存信息</el-button>
                            </el-form-item>
                        </el-form>
                    </el-card>
                </el-col>
            </el-row>
        </el-main>
        <el-footer style="height: 280px;background-color: #2f3234;padding:50px 0">
            <my-footer></my-footer>
        </el-footer>
    </el-container>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<!--引入Axios-->
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
<!--引入自己的js文件-->
<script src="js/my-header.js"></script>
<script src="js/my-footer.js"></script>

<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                user:localStorage.user?JSON.parse(localStorage.user):null,
                url:""
            }
        },
        methods: {
            handleSuccess(response,file,fileList){//当上传成功时此方法会被执行
                v.url = response.data;
            },
            save() {
                let u = {id:v.user.id,nickName:v.user.nickName}
                if (v.url!=""){//选择了图片
                    u.imgUrl = v.url;

                }
                //把新的昵称和图片路径保存到数据库中
                axios.post("/v1/users/update",u).then(function (response) {
                    if (response.data.code==1){
                        if (v.url!=""){
                            v.user.imgUrl = v.url;//把图片路径赋值给当前登录的用户对象
                        }
                        localStorage.user = JSON.stringify(v.user);
                        location.reload();
                    }
                    
                })

            },
            handleRemove(file, fileList) {
                v.url="";
                console.log(file, fileList);
                //发出删除文件的请求   file.response上传成功时服务器响应的内容ResultVO
                console.log(file.response);
                axios.get("/v1/upload/remove?url="+file.response.data).then(function () {
                    console.log("服务器文件删除完成!");
                })

            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            }
        },
        created:function () {
            
        }
    })
</script>
</html>